< previous page page_151 next page >

Page 151
a path such as C:\Data\Word and presses Enter, the dialog box is updated to show that folder. Also, if the user single-clicks a filename in the file/folder selection area, it appears here.
File Type list box. Here, the user selects the types of files to display. These types are determined by the extension portion of the filename; the available types are controlled by the Filter property of the CommonDialog control.
Toolbar buttons and Command buttons. The buttons in the upper-right corner let the user move up one folder level, create a new folder, or switch the file display area between the list mode and the file details mode. The buttons at the lower-right corner let the user process the selection or cancel the dialog box.
Opening and Saving Files
To open an existing file, use the ShowOpen method of the CommonDialog control. (This method displays the dialog box in Figure 7.6.) You use this method by specifying the name of the CommonDialog control and the method name. To try it, type this code into the Click event procedure of the command button cmdTest in the sample project:
cdlTest.ShowOpen
Msgbox "You Selected " & cdlTest.FileName & " to be opened."
After the preceding code is executed, the name of the file selected is available to your code through the CommonDialog control's FileName property.
Using the CommonDialog control for saving and opening involves essentially the same steps. The name of the method used to invoke the Save As dialog box is ShowSave. You will find a few subtle differences between the dialog boxes shown for the Open and Save functions, such as the title of the dialog box and the captions on the command buttons.
To test the CommonDialog control's ShowSave method, replace the code in the sample project's cmdTest_Click procedure with these lines:
cdlTest.ShowSave
Msgbox "You Selected " & cdlTest.FileName & " to be saved."
note.gif The Open and Save As dialog boxes don't actually open or save files; they simply get information from the user as to the names and locations of the files to be opened or saved. Your program must take whatever steps are necessary to complete the operation. square.gif

Specifying File Types with the Filter Property
When using the CommonDialog control, you might need to specify that only certain file types are listed. If your program reads Microsoft Excel (.XLS) files, for example, you would not want the user to attempt to open batch (.BAT) files. You can restrict (or ''filter") the files shown in the dialog box by using the Filter property.
You set the Filter property at designtime from either the Properties window or the Property Pages dialog box, or at runtime with an assignment statement in code. The Filter property is a string value that includes a file type description followed by the file extension. It requires a special format, as shown here:
cldTest.Filter = "Word Documents (*.doc)|*.doc"

 
< previous page page_151 next page >